package edu.ustb.controller;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;



import edu.ustb.domain.Address;
import edu.ustb.domain.Cart;
import edu.ustb.domain.Goods;
import edu.ustb.domain.Order;
import edu.ustb.domain.User;
import edu.ustb.mapper.OrderMapper;
import edu.ustb.service.CartIndent;
import edu.ustb.service.CartIndentImpl;
import edu.ustb.utils.MD5Util;
import edu.ustb.utils.MybatisUtil;

@WebServlet("/CartToPay.do")
public class OrderSubmitController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		//设置请求数据的编码格式
		request.setCharacterEncoding("utf-8");
		System.out.println("1");
		//从提交订单页面获取到购物车中的id信息和对应的商品id信息
		String []checkid1=request.getParameterValues("orderGoodsId");
	    List<String> orderGoodsId = new ArrayList<String>();
	    for (int i=0;i<checkid1.length;i++) {
	    	orderGoodsId.add(checkid1[i]);
		} 
	    String []checkid2=request.getParameterValues("orderItemId");
	    List<String> orderItemId = new ArrayList<String>();
	    for (int i=0;i<checkid2.length;i++) {
	    	orderItemId.add(checkid2[i]);
		} 
		
		//如果是从购物车界面跳转到订单界面，那么获取复选框的选取的地址id
		String []checkid3=request.getParameterValues("orderAddressId");
	    List<String> orderAddressId = new ArrayList<String>();
	    for (int i=0;i<checkid3.length;i++) {
	    	orderAddressId.add(checkid3[i]);
		}  
	    System.out.println(checkid1);
	    System.out.println(checkid2);
	    System.out.println(checkid3);

		CartIndent cartIndent=new CartIndentImpl();
		List<Goods> goodsList =cartIndent.goodsInCartIndent(orderGoodsId);
		List<Cart> cartList =cartIndent.cartToIndent(orderItemId);
		List<Address> addressList=cartIndent.addressToIndent(orderAddressId);
		System.out.println(goodsList);
		System.out.println(cartList);
		System.out.println(addressList);
		
		
		//获取当前用户的id
		User user = (User) request.getSession().getAttribute("user");
		int orderUser=user.getUserId();
		String orderAddress=user.getUserAddress();
		String orderPhone=user.getUserPhone();
		String orderName=user.getUserName();
		int orderStatus=1;
		
		String orderPrice=request.getParameter("sumPrice");
						
		//Order order=new Order(orderId,orderUser,orderAddress,orderPhone,orderName,orderStatus,orderPrice,null);
		Order order=new Order();
		order.setOrderAddress(orderAddress);
		order.setOrderName(orderName);
		order.setOrderPhone(orderPhone);
		order.setOrderPrice(Float.parseFloat(orderPrice));
		order.setOrderStatus(orderStatus);
		order.setOrderTime(new Date());
		order.setOrderUser(orderUser);
		//随机生成一个订单
		String orderId = MD5Util.getMd5(String.valueOf(order.getOrderTime()));
		order.setOrderId(orderId);
		request.setAttribute("order", order);
		System.out.println(order);
		//request.getRequestDispatcher("/Cart-pay.html").forward(request, response);	
		try{	
			SqlSession openSession = MybatisUtil.open();
			OrderMapper mapper = openSession.getMapper(OrderMapper.class);
			int rows=mapper.addOrder(order);
			if (rows>0){
				System.out.println("rows>0");	
				request.getRequestDispatcher("/Cart-pay.jsp").forward(request, response);
					
				}	
		}catch (Exception e) {					
			e.printStackTrace();		
		}
	}
}